<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['admin:assets:maintenances:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['admin:assets:maintenances:put']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['admin:assets:maintenances:delete']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          v-hasPermi="['admin:assets:assets:export']"-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-position"-->
<!--          size="mini"-->
<!--          :loading="exportLoading"-->
<!--          @click="handleInput"-->
<!--        >导入</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          v-hasPermi="['admin:assets:maintenances:export']"-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          :loading="exportLoading"-->
<!--          @click="handleExport"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="componentsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column width="155" label="资产名称" align="center" prop="asset_name" sortable />
      <el-table-column width="155" label="资产标签" align="center" prop="asset_tag" sortable />
      <el-table-column width="155" label="资产维护标题" align="center" prop="title" sortable />
      <el-table-column width="155" label="资产位置" align="center" prop="location_name" sortable />
      <el-table-column width="155" label="资产维护类型" align="center" prop="asset_maintenance_type" sortable />
      <el-table-column width="155" label="开始时间" align="center" prop="start_date" sortable />
      <el-table-column width="155" label="维修周期（天）" align="center" prop="asset_maintenance_time" sortable />
      <el-table-column width="155" label="备注" align="center" prop="notes" sortable />
      <el-table-column width="155" label="维修费用" align="center" prop="cost" sortable />
      <el-table-column width="155" label="供应商" align="center" prop="supplier_name" sortable />
      <el-table-column fixed="right" width="155" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['admin:assets:maintenances:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['admin:assets:maintenances:delete']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资产维护表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item v-if="!this.assets_id" label="选择资产标签" prop="asset">
          <el-select v-model="form.asset" placeholder="请选择资产标签" clearable size="small">
            <el-option
              v-for="dict in assetList"
              :key="dict.id"
              :label="dict.asset_tag"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="供应商" prop="supplier">
          <el-select v-model="form.supplier" placeholder="请选择供应商" clearable size="small">
            <el-option
              v-for="dict in suppliersList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="资产维护类型" prop="asset_maintenance_type">
          <el-select v-model="form.asset_maintenance_type" placeholder="请选择资产维护类型" clearable size="small">
            <el-option
              v-for="dict in maintainTypeList"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入资产构件名称" />
        </el-form-item>
        <el-form-item label="开始时间" prop="start_date">
          <el-date-picker
            v-model="form.start_date"
            clearable
            size="small"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择采购日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="维修周期（天）" prop="asset_maintenance_time">
          <el-input v-model="form.asset_maintenance_time" placeholder="请输入维修周期" />
        </el-form-item>
        <el-form-item label="维护费用" prop="cost">
          <el-input v-model="form.cost" placeholder="请输入维护费用" />
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input v-model="form.notes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMaintenances,
  getMaintenances,
  delMaintenances,
  addMaintenances,
  updateMaintenances,
  exportMaintenances,
  listAsset
} from "@/api/vadmin/assets/assets";
import FileUpload from "@/components/FileUpload/index";
import { listCategories } from "@/api/vadmin/basic/categories";
import { listManufacturers } from "@/api/vadmin/basic/manufacturers";
import { listSuppliers } from "@/api/vadmin/basic/suppliers";

export default {
  name: "Maintain",
  components: { FileUpload },
  props: {
    assets_id: Number,
    // eslint-disable-next-line vue/require-prop-type-constructor
    default: 0
  },
  data() {
    return {
      title1: "",
      open1: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资产维护表表格数据
      componentsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      manufacturersList: [],
      categoriesList: [],
      suppliersList: [],
      assetList: [],
      maintainTypeList: [],
      // 表单校验
      rules: {
        asset: [
          { required: true, message: "请先选中需要维护的资产标签", trigger: "change" }
        ]
      }
    };
  },
  created() {
  },
  mounted() {
    this.getSuppliers();
    listAsset().then(response => {
      this.assetList = response.data.results;
    });
    this.getDicts("maintain_type").then(response => {
      this.maintainTypeList = response.data;
      this.queryParams.asset = this.assets_id;
      this.getList();
    });
  },
  methods: {
    getCategoriesList() {
      this.loading = true;
      listCategories().then(response => {
        this.categoriesList = response.data.results;
        this.loading = false;
      });
    },
    getMaufacturersList() {
      this.loading = true;
      listManufacturers().then(response => {
        this.manufacturersList = response.data.results;
        this.loading = false;
      });
    },
    getSuppliers() {
      this.loading = true;
      listSuppliers().then(response => {
        this.suppliersList = response.data.results;
        this.loading = false;
      });
    },
    /** 查询资产维护表列表 */
    getList() {
      this.loading = true;
      listMaintenances(this.queryParams).then(response => {
        this.componentsList = response.data.results;
        this.total = response.data.count;
        this.loading = false;
        this.$emit("updateNumber", this.total);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        description: null,
        modifier: null,
        dept_belong_id: null,
        update_datetime: null,
        create_datetime: null,
        serial: null,
        name: null,
        deleted_datetime: null,
        qty: null,
        purchase_date: null,
        purchase_cost: null,
        order_number: null,
        min_amt: null,
        image: null,
        notes: null,
        category_id: null,
        creator_id: null,
        manufacturer_id: null,
        supplier_id: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加资产维护表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getMaintenances(id).then(response => {
        this.form = response.data;
        this.form.asset = this.assets_id;
        this.open = true;
        this.title = "修改资产维护表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMaintenances(this.form).then(response => {
              this.$message({
                message: "修改成功",
                type: "success"
              });
              this.open = false;
              this.getList();
            });
          } else {
            if (this.assets_id) {
              this.form.asset = this.assets_id;
            }
            addMaintenances(this.form).then(response => {
              this.$message({
                message: "新增成功",
                type: "success"
              });
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        delMaintenances(ids).then(res => {
          this.getList();
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        });
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除"
        });
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm("是否确认导出所有资产维护表数据项？").then(() => {
        this.exportLoading = true;
        return exportMaintenances(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
<style>
.avatar {
  width: 178px;
  height: 178px;
  display: block;
  margin-top: 15px;
}
</style>
